<?php
/**
 * Created by PhpStorm.
 * User: simon
 * Date: 2016/11/20
 * Time: 8:49
 */

namespace App\Weixin;


use App\Common\WeixinTokenCacher;
use App\Models\WeixinToken;
use Illuminate\Support\Facades\Log;
use p5\objects\TimeUtils;

class WechatClient extends Wechat
{
    const COMPONENT_VERIFY_TICKET = "component_verify_ticket";
    const COMPONENT_ACCESS_TOKEN = "component_access_token";
    const COMPONENT_PRE_AUTH_CODE = "pre_auth_code";
    const COMPONENT_AUTHORIZATION_INFO = "authorization_info";
    const COMPONENT_AUTHORIZER_INFO = "authorizer_info";
    const GET_INDUSTRY_URL = "/cgi-bin/template/get_industry?";
    const GET_TEMPLATE_URL = "/cgi-bin/template/get_all_private_template?";


    /**
     * 获得所属行业
     */
    public function getTMIndustry()
    {
        if (!$this->access_token && !$this->checkAuth()) return false;
        $result = $this->http_get(self::API_BASE_URL_PREFIX . self::GET_INDUSTRY_URL . 'access_token=' . $this->access_token);
        if ($result) {
            Log::debug("get industry result [" . $result . "]");
            $this->parseContent($result);
        }
        return $this->_receive;
    }

    /**
     * 获得公众好的模板消息配置
     */
    public function getTMTemplates()
    {
        if (!$this->access_token && !$this->checkAuth()) return false;
        $result = $this->http_get(self::API_BASE_URL_PREFIX . self::GET_TEMPLATE_URL . 'access_token=' . $this->access_token);
        if ($result) {
            Log::debug("get templates result [" . $result . "]");
            $this->parseContent($result);
        }
        return $this->_receive;
    }

    public function parseContent($content = "")
    {
        if (empty($content)) {
            if ($_SERVER['REQUEST_METHOD'] == "POST") {
                $content = file_get_contents("php://input");
            }
        }
        $content = trim($content);
        if (empty($content)) {
            $this->postxml = $content;
            Log::debug("内容为空");
            return $this->getRev();
        }

        $first = substr($content, 0, 1);
        if ($first == "{") {//JSON 格式
            Log::debug("是JSON格式的内容:" . $content);
            $this->_receive = json_decode($content, true);
            return $this;
        }

        $array = (array)simplexml_load_string($content, 'SimpleXMLElement', LIBXML_NOCDATA);
        $this->encrypt_type = isset($_GET["encrypt_type"]) ? $_GET["encrypt_type"] : '';
        if ($this->encrypt_type == 'aes') { //aes加密
            $encryptStr = $array['Encrypt'];
            $pc = new Prpcrypt($this->encodingAesKey);
            $array = $pc->decrypt($encryptStr, $this->appid);
            if (!isset($array[0]) || ($array[0] != 0)) {
                Log::error("内容解密失败!");
                Log::error("appid is " . $this->appid);
                return false;
            }
            $this->postxml = $array[1];
            Log::debug("解密后的内容体");
            Log::debug($this->postxml);
            if (!$this->appid) {
                $this->appid = $array[2];   //为了没有appid的订阅号。
            }
        } else {
            $this->postxml = $content;
        }
        $this->getRev();
    }
}